﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Events;

namespace VehicleMaintenance.BaseInfo
{
    public partial class ReviseInventoryForm : BaseForm
    {
        private int goodID = 0;

        public ReviseInventoryForm(int id)
        {
            InitializeComponent();

            goodID = id;
        }

        private void ReviseInventoryForm_Load(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();
                Goods good = db.Goods.First(p => p.GoodID == goodID);
                lbCode.Text = good.Code;
                lbName.Text = good.Name;
                lbNumber.Text = good.Number.ToString();
                lbUnit.Text = good.Units;
                lbUnit2.Text = good.Units;

                txtNewNumber.Text = good.Number.ToString();
                txtNewNumber.Focus();
				txtNewNumber.KeyPress += new KeyPressEventHandler(KeyEvents.Number_KeyPress);
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtNewNumber.Text.Trim()))
                {
                    this.ShowInfo("请输入新库存");
                    txtNewNumber.Focus();
                    return;
                }
                CarDBContext db = new CarDBContext();
                Goods good = db.Goods.First(p => p.GoodID == goodID);
                good.Number = Convert.ToInt32(txtNewNumber.Text.Trim());
                db.SaveChanges();

                this.ShowInfo("库存调整成功");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }
        
    }
}
